package com.smartcity.controller;


import cn.hutool.core.bean.BeanUtil;
import com.smartcity.domain.dto.PageDTO;
import com.smartcity.domain.dto.UserDTO;
import com.smartcity.domain.po.User;
import com.smartcity.domain.query.UserQuery;
import com.smartcity.domain.vo.UserVO;
import com.smartcity.service.IUserService;
import com.smartcity.result.ResultBase;
import com.smartcity.result.ResultList;
import com.smartcity.utils.BizException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2024-08-17
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    ResultList<UserVO> getUserList() {
        List<User> list = userService.list();
        List<UserVO> userVOS = BeanUtil.copyToList(list, UserVO.class);
        return ResultList.successs(userVOS, list.size());
    }

    @Operation(summary = "添加用户")
    @PostMapping()
    ResultBase addUser(@RequestBody UserDTO userDTO) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        boolean save = userService.save(user);
        if (save) {
            return ResultBase.success();
        } else {
            return ResultBase.error("添加失败");
        }
    }

    @Operation(summary = "分页返回用户信息")
    @GetMapping("/page")
    ResultList<UserDTO> userPage(@Parameter(description = "用户表单") UserQuery userQuery) {
        System.out.println(userQuery.toString());
        List<UserDTO> userDTOS = userService.queryUserPage(userQuery);
        return ResultList.successs(userDTOS, userDTOS.size());

    }

    @Operation(summary = "test")
    @GetMapping("/test")
    ResultBase test() {
        throw new BizException("自定义异常", new Throwable("自定义异常信息"));
    }
}
